<template>
  <div class="admin-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="240px" class="sidebar">
        <div class="logo">
          <h3>视频管理系统</h3>
        </div>
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical-dark"
          router
        >
          <el-menu-item index="/dashboard">
            <el-icon><Document /></el-icon>
            <span>仪表盘</span>
          </el-menu-item>
          <el-menu-item index="/categories">
            <el-icon><Menu /></el-icon>
            <span>视频类别管理</span>
          </el-menu-item>
          <el-menu-item index="/video-types">
            <el-icon><VideoCamera /></el-icon>
            <span>视频类型管理</span>
          </el-menu-item>
          <el-menu-item index="/videos">
            <el-icon><VideoPlay /></el-icon>
            <span>视频管理</span>
          </el-menu-item>
          <el-menu-item index="/comments">
            <el-icon><ChatRound /></el-icon>
            <span>评论管理</span>
          </el-menu-item>
          <el-menu-item index="/users">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="/settings">
            <el-icon><Setting /></el-icon>
            <span>系统设置</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header class="header">
          <div class="header-left">
            <span>管理员控制台</span>
          </div>
          <div class="header-right">
            <span class="user-info">{{ userInfo.username }}</span>
            <el-button type="text" @click="handleLogout">
              <el-icon><SwitchButton /></el-icon>
              退出登录
            </el-button>
          </div>
        </el-header>

        <!-- 内容区 -->
        <el-main class="main-content">
          <div class="management-content">
            <h2>用户管理</h2>
            <div class="section-title">管理平台的所有用户</div>
            
            <div class="search-bar">
              <el-input
                v-model="searchQuery"
                placeholder="搜索用户名或邮箱"
                prefix-icon="el-icon-search"
                style="width: 400px;"
              ></el-input>
              <el-button type="primary" @click="handleAddUser">
                <el-icon><Plus /></el-icon>
                添加用户
              </el-button>
            </div>

            <el-table :data="filteredUsers" style="width: 100%">
              <el-table-column prop="username" label="用户" min-width="200">
                <template #default="scope">
                  <div class="user-info-cell">
                    <el-avatar :size="40" :src="`https://api.dicebear.com/7.x/bottts/svg?seed=${scope.row.username}`"></el-avatar>
                    <div class="user-details">
                      <div>{{ scope.row.username }}</div>
                      <div class="user-email">{{ scope.row.email }}</div>
                    </div>
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="role" label="角色" width="120">
                <template #default="scope">
                  <el-tag
                    :type="scope.row.role === 'admin' ? 'primary' : scope.row.role === 'creator' ? 'success' : 'info'"
                    size="small"
                  >
                    {{ scope.row.role === 'admin' ? '管理员' : scope.row.role === 'creator' ? '创作者' : '观众' }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="status" label="状态" width="100">
                <template #default="scope">
                  <el-tag
                    :type="scope.row.status === 'active' ? 'success' : 'danger'"
                    size="small"
                  >
                    {{ scope.row.status === 'active' ? '活跃' : '已禁用' }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="videoCount" label="视频数" width="100"></el-table-column>
              <el-table-column prop="followerCount" label="粉丝数" width="100"></el-table-column>
              <el-table-column prop="joinDate" label="加入日期" width="120"></el-table-column>
              <el-table-column label="操作" width="120" fixed="right">
                <template #default="scope">
                  <el-dropdown>
                    <el-button type="text" size="small">
                      <el-icon><More /></el-icon>
                    </el-button>
                    <template #dropdown>
                      <el-dropdown-menu>
                        <el-dropdown-item @click="handleEditUser">编辑</el-dropdown-item>
                        <el-dropdown-item @click="handleToggleStatus(scope.row)" :disabled="scope.row.role === 'admin'">
                          {{ scope.row.status === 'active' ? '禁用' : '启用' }}
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </template>
                  </el-dropdown>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { Document, User, Menu, VideoPlay, ChatRound, Setting, SwitchButton, Plus, More } from '@element-plus/icons-vue';

interface User {
  id: number;
  username: string;
  email: string;
  nickname: string;
  role: 'admin' | 'creator' | 'viewer';
  status: 'active' | 'disabled';
  videoCount: number;
  followerCount: number;
  joinDate: string;
}

const router = useRouter();
const userInfo = ref({ username: '', nickname: '', role: '' });
const searchQuery = ref('');

const users = ref<User[]>([
  { id: 1, username: '张三', email: 'zhangsan@example.com', nickname: '张三', role: 'creator', status: 'active', videoCount: 45, followerCount: 12000, joinDate: '2023-05-15' },
  { id: 2, username: '李四', email: 'lisi@example.com', nickname: '李四', role: 'creator', status: 'active', videoCount: 89, followerCount: 45000, joinDate: '2023-03-20' },
  { id: 3, username: '王五', email: 'wangwu@example.com', nickname: '王五', role: 'viewer', status: 'active', videoCount: 0, followerCount: 234, joinDate: '2024-01-10' },
  { id: 4, username: '赵六', email: 'zhaoliu@example.com', nickname: '赵六', role: 'creator', status: 'disabled', videoCount: 23, followerCount: 8900, joinDate: '2023-08-05' },
  { id: 5, username: '孙七', email: 'sunqi@example.com', nickname: '孙七', role: 'creator', status: 'active', videoCount: 12, followerCount: 3400, joinDate: '2023-02-18' },
  { id: 6, username: '周八', email: 'zhouba@example.com', nickname: '周八', role: 'viewer', status: 'active', videoCount: 0, followerCount: 567, joinDate: '2024-06-22' }
]);

const activeMenu = computed(() => {
  return window.location.pathname;
});

const filteredUsers = computed(() => {
  if (!searchQuery.value) return users.value;
  
  const query = searchQuery.value.toLowerCase();
  return users.value.filter(user => 
    user.username.toLowerCase().includes(query) || 
    user.email.toLowerCase().includes(query)
  );
});

const initData = () => {
  const userStr = localStorage.getItem('user');
  if (userStr) {
    userInfo.value = JSON.parse(userStr);
  }
};

const handleLogout = () => {
  localStorage.removeItem('token');
  localStorage.removeItem('user');
  ElMessage.success('退出登录成功');
  router.push('/login');
};

const handleAddUser = () => {
  ElMessage.info('添加用户功能开发中');
};

const handleEditUser = () => {
  ElMessage.info('编辑用户功能开发中');
};

const handleToggleStatus = (user: User) => {
  user.status = user.status === 'active' ? 'disabled' : 'active';
  ElMessage.success(`${user.username} 用户状态已更新`);
};

onMounted(() => {
  initData();
});
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  background-color: #1a1a1a;
}

.sidebar {
  background-color: #2a2a2a;
  height: 100vh;
}

.logo {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid #3a3a3a;
}

.logo h3 {
  color: #ffffff;
  margin: 0;
}

.el-menu-vertical-dark {
  background-color: transparent;
  border-right: none;
}

.el-menu-item {
  color: #cccccc;
  margin: 0 10px;
  border-radius: 4px;
}

.el-menu-item:hover {
  background-color: #3a3a3a;
  color: #ffffff;
}

.el-menu-item.is-active {
  background-color: #409eff;
  color: #ffffff;
}

.header {
  background-color: #2a2a2a;
  color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  border-bottom: 1px solid #3a3a3a;
}

.user-info {
  margin-right: 20px;
  color: #cccccc;
}

.main-content {
  background-color: #1a1a1a;
  padding: 20px;
  overflow: auto;
}

.management-content h2 {
  color: #ffffff;
  margin-bottom: 5px;
}

.section-title {
  color: #999999;
  margin-bottom: 20px;
  font-size: 14px;
}

.search-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.user-info-cell {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-details {
  display: flex;
  flex-direction: column;
}

.user-email {
  font-size: 12px;
  color: #999999;
}

.el-table {
  background-color: #2a2a2a;
  border-radius: 8px;
  overflow: hidden;
}

.el-table th {
  background-color: #3a3a3a;
  color: #ffffff;
  border-bottom: 1px solid #4a4a4a;
}

.el-table tr {
  background-color: #2a2a2a;
}

.el-table td {
  color: #cccccc;
  border-bottom: 1px solid #3a3a3a;
}

.el-table tr:hover > td {
  background-color: #3a3a3a;
}
</style>